package otherapi.itext.kernellayoutpdf;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.utils.PdfMerger;
import org.junit.Test;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 11/2/2022 5:41 PM
 */
public class ItextEasyExcelTest {


    private static final String FILE_NAME1 = "demo.pdf";
    private static final String FILE_NAME2 = "demo1.pdf";
    private static final String DEST = "MergeDemo.pdf";
    private static final String DEST_FOLDER = "./file/pdftest";

    /**
     * 使用 copyPagesTo 合并
     *
     * @throws IOException ioexception
     */
    @Test
    public void test1() throws IOException {
        PdfWriter pdfWriter = new PdfWriter(DEST);
        // 最终合并的 dpf
        PdfDocument pdfDocument = new PdfDocument(pdfWriter);

        PdfReader pdfReader1 = new PdfReader(FILE_NAME1);
        PdfDocument pdfDocument1 = new PdfDocument(pdfReader1);
        pdfDocument1.copyPagesTo(1, pdfDocument1.getNumberOfPages(), pdfDocument);

        PdfReader pdfReader2 = new PdfReader(FILE_NAME2);
        PdfDocument pdfDocument2 = new PdfDocument(pdfReader2);
        pdfDocument2.copyPagesTo(1, pdfDocument2.getNumberOfPages(), pdfDocument);

        pdfReader1.close();
        pdfDocument1.close();
        pdfReader2.close();
        pdfDocument2.close();
        pdfDocument.close();
    }

    /**
     * 使用  pdfMerger.merge
     *
     * @throws IOException ioexception
     */
    @Test
    public void test2() throws IOException {
        PdfWriter pdfWriter = new PdfWriter(DEST);
        // 最终合并的 dpf
        PdfDocument pdfDocument = new PdfDocument(pdfWriter);
        PdfMerger pdfMerger = new PdfMerger(pdfDocument);

        PdfReader pdfReader1 = new PdfReader(FILE_NAME1);
        PdfDocument pdfDocument1 = new PdfDocument(pdfReader1);
        pdfMerger.merge(pdfDocument1, 1, pdfDocument1.getNumberOfPages());

        PdfReader pdfReader2 = new PdfReader(FILE_NAME2);
        PdfDocument pdfDocument2 = new PdfDocument(pdfReader2);
        pdfMerger.merge(pdfDocument2, 1, pdfDocument2.getNumberOfPages());

        pdfReader1.close();
        pdfDocument1.close();
        pdfReader2.close();
        pdfDocument2.close();
        pdfDocument.close();
    }

    /**
     * 每个文件 1 页 分割
     *
     * @throws IOException ioexception
     */
    @Test
    public void test3() throws IOException {
        pdfSplitter(DEST, 1, DEST_FOLDER);
    }


    /**
     * 在指定目录等分pdf
     *
     * @param fileName 要分割的文档
     * @param pageNum  分割尺寸
     * @param desDir   分割后存储路径
     * @throws IOException ioexception
     */
    private void pdfSplitter(String fileName, Integer pageNum, String desDir) throws IOException {
        PdfReader pdfReader = new PdfReader(fileName);
        PdfDocument pdf = new PdfDocument(pdfReader);
        String name;
        PdfWriter pdfWriter;
        PdfDocument pdfWriterDoc;
        int numberOfPages = pdf.getNumberOfPages();
        for (int i = 1; i <= numberOfPages; i += pageNum) {
            name = desDir + "/" + i + ".pdf";
            File file = new File(name);
            if (!file.exists()) {
                file.createNewFile();
            }
            pdfWriter = new PdfWriter(name);
            pdfWriterDoc = new PdfDocument(pdfWriter);
            int end = Math.min((i + pageNum - 1), pdf.getNumberOfPages());
            //从页数第一页开始，
            pdf.copyPagesTo(i, end, pdfWriterDoc);
            pdfWriterDoc.close();
            pdfWriter.close();
        }
        //关闭
        pdf.close();
        pdfReader.close();

    }

    /**
     * 分割文档，分割后文仔默认存储在原来的文档路径下。
     *
     * @param fileName
     * @param pageNum
     * @throws IOException
     */
    private void pdfSplitter(String fileName, Integer pageNum) throws IOException {
        String desDir = new File(fileName).getParentFile().getAbsolutePath();
        pdfSplitter(fileName, pageNum, desDir);
    }

    /**
     * 返回自定义片段大小的文件，UUID名称命名。
     *
     * @param fileName
     * @param startPage
     * @param endPage
     * @throws IOException
     */
    public void pdfSplitter(String fileName, int startPage, int endPage) throws IOException {
        //源文档
        PdfReader pdfReader = new PdfReader(fileName);
        PdfDocument pdf = new PdfDocument(pdfReader);
        //目标文档名
        String desDir = new File(fileName).getParentFile().getAbsolutePath() + "/" + UUID.randomUUID() + ".pdf";
        //生成目标文档
        PdfWriter pdfWriter = new PdfWriter(desDir);
        PdfDocument outPdfDocument = new PdfDocument(pdfWriter);
        //从页数第一页开始，
        pdf.copyPagesTo(startPage, endPage, outPdfDocument);
        //关闭
        outPdfDocument.close();
        pdfWriter.close();
        pdf.close();
        pdfReader.close();
    }

}
